
import RuleChainApi from '@/api/ruleChain'
import store from '@/store'

// 节点冲突检测
export async function nodeClashValidate(nodeList, graph) {
  const sceneList = []
  const serviceList = []
  const clashList = store.state.clash.clashList
  nodeList.forEach(node => {
    clearNodeClashValidate(node) // 清空该节点所有校验信息
    const param = { nodeName: node.data.name, configuration: node.data.configuration, nodeId: node.id }
    if (node.data.configuration) {
      // 对节点分类
      if (['SceneLibSceneNode', 'SceneLibFrontConditionNode', 'SceneLibAutoTouchNode', 'SceneLibAutoExitNode'].includes(node.data.additionalInfo.component)) {
        sceneList.push(param)
      } else if (node.data.additionalInfo.component === 'ServiceLibServiceNode') {
        serviceList.push(param)
      }
    }
  })

  // const res = await Promise.all([RuleChainApi.sceneNodeCheck(sceneList), RuleChainApi.serviceNodeCheck(serviceList)])
  // if (sceneList.length && res[0].data?.length) {
  //   res[0].data.forEach(clashItem => {
  //     const node = graph.getNodes().find(item => (item.id === clashItem.nodeId))
  //     clashList.push({ msg: clashItem.msg, type: clashItem.errorType === '0' ? 'warning' : 'error', node, validTime: 'node' })
  //   })
  // }
  // if (serviceList.length && res[1].data?.length) {
  //   res[1].data.forEach(clashItem => {
  //     const node = graph.getNodes().find(item => (item.id === clashItem.nodeId))
  //     clashList.push({ msg: clashItem.msg, type: clashItem.errorType === '0' ? 'warning' : 'error', node, validTime: 'node' })
  //   })
  // }
  store.dispatch('clash/setClashList', clashList)
}

// 删除节点的冲突提示信息
export function clearNodeClashValidate(node) {
  const clashList = store.state.clash.clashList
  const index = clashList.findIndex(clash => clash.node?.id === node.id && clash.validTime === 'node')
  if (index > -1) {
    clashList.splice(index, 1)
    store.dispatch('clash/setClashList', clashList)
    if (clashList.find(clash => clash.node?.id === node.id && clash.validTime === 'node')) {
      clearNodeClashValidate(node) // 递归删除
    }
  }
}

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str) {
  const validMap = ['admin', 'editor']
  return validMap.indexOf(str.trim()) >= 0
}

/**
 * @param {string} url
 * @returns {Boolean}
 */
export function validURL(url) {
  const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return reg.test(url)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validLowerCase(str) {
  const reg = /^[a-z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUpperCase(str) {
  const reg = /^[A-Z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validAlphabets(str) {
  const reg = /^[A-Za-z]+$/
  return reg.test(str)
}

/**
 * @param {string} email
 * @returns {Boolean}
 */
export function validEmail(email) {
  // eslint-disable-next-line no-useless-escape
  const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  return reg.test(email)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function isString(str) {
  if (typeof str === 'string' || str instanceof String) {
    return true
  }
  return false
}

/**
 * @param {Array} arg
 * @returns {Boolean}
 */
export function isArray(arg) {
  if (typeof Array.isArray === 'undefined') {
    return Object.prototype.toString.call(arg) === '[object Array]'
  }
  return Array.isArray(arg)
}
